import { getInfo, loginByCode, setInfo, upload } from '@/api';
import { useAuth } from '@/little/hooks';

export const useMember = () => {
    const info = {};
    const handler = {
        get(target, prop, receiver) {
            return Reflect.get(...arguments);
        }
    };
    const { setAuth } = useAuth();
    /**
     * 会员信息
     */
    const data = new Proxy(info, handler);
    const setMemberInfo = (args) => {
        data.value = args;
    };
    /**
     * 微信 unionId 登录
     */
    const loginByUnionId = () => {
        uni.login({
            provider: 'weixin',
            success: (res) => {
                hendleLogin({ code: res.code });
            }
        });
        // uni.checkSession({
        //   fail: (_err) => {
        //     uni.login({
        //       provider: 'weixin',
        //       success: (res) => {
        //         hendleLogin({ code: res.code });
        //       },
        //     });
        //   },
        //   complete: (_res) => {
        //     // isLogin.value &&
        //     uni.login({
        //       provider: 'weixin',
        //       success: (res) => {
        //         hendleLogin({ code: res.code });
        //       },
        //     });
        //   },
        // });
    };
    /**
     * 微信 code 换取手机号 登录
     */
    const loginByMobile = (args) => {
        if (args?.detail?.encryptedData) {
            const { code, encryptedData: encrypted, iv } = args.detail;
            hendleLogin({ code, encrypted, iv });
        } else {
            const { code, encrypted, iv } = args;
            hendleLogin({ code, encrypted, iv });
        }
    };

    /**
     * 处理登录
     */
    const hendleLogin = (args) => {
        console.log(args);
        loginByCode(args?.code ? args.code : args)
            .then((res) => {
                console.debug(res);
                const info = res.data;
                setAuth(info.token);
                // hendleGetInfo();
            })
            .catch((err) => {
                console.debug('login err:', err);
            });
    };
    /**
     * 微信code 换取手机号然后设置
     */
    const hendleBindMobile = (args) => {
        const { code } = args.detail;
        codeByMobile(code).then((res) => {
            console.debug(res);
        });
    };
    /**
     * 设置头像
     */
    const setAvatar = (args) => {
        console.debug(args);
        const { avatarUrl: avatar } = args.detail;
        upload('oss', 'single', {
            filePath: avatar,
            name: 'avatar'
        }).then((res) => {
            console.log('UploadResponse', res);
            const { id } = res.data;
            hendleSetInfo({ meta: { avatar: id } });
        });
        console.debug(avatar);
    };
    /**
     * 设置昵称
     */
    const setNickName = (args) => {
        const { value: nickname } = args.detail;
        console.debug(data.nickname, nickname);
        nickname && hendleSetInfo({ nickname });
        nickname && closeNickNameInput();
    };

    /**
     * 设置会员信息
     */
    const hendleSetInfo = async (args) => {
        const result = await setInfo(args);
        console.debug('hendleSetInfo result:', result);
        hendleGetInfo();
    };
    /**
     * 获取用户信息
     */
    const hendleGetInfo = () => {
        isLogin.value &&
            getInfo().then(({ data }) => {
                setMemberInfo(data);
            });
        return data;
    };

    return {
        data,
        member: data,
        loginByUnionId,
        loginByMobile,
        setMemberInfo,
        hendleBindMobile,
        hendleSetInfo,
        hendleGetInfo,
        setNickName,
        setAvatar
    };
};
